package com.water.skin.attr

import android.content.res.Resources.NotFoundException
import android.view.View
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import com.water.skin.ResourceManager
import com.water.skin.SkinManager
import pl.droidsonroids.gif.GifDrawable
import pl.droidsonroids.gif.GifImageView

/**
 * Created by zhy on 15/9/28.
 */
enum class SkinAttrType(var attrType: String) {

    BACKGROUND("background") {

        override fun apply(view: View, resName: String) {
            val drawable = resourceManager().getDrawableByName(resName)
            if (drawable != null) {
                view.setBackgroundDrawable(drawable)
            } else {
                try {
                    val color = resourceManager().getColor(resName)
                    view.setBackgroundColor(color)
                } catch (ex: NotFoundException) {
                    ex.printStackTrace()
                }
            }
        }
    },
    COLOR("textColor") {
        override fun apply(view: View, resName: String) {
            val colorList = resourceManager().getColorStateList(resName) ?: return
            (view as TextView).setTextColor(colorList)
        }
    },
    SRC("src") {
        override fun apply(view: View, resName: String) {
            if (view is ImageView) {
                val drawable = resourceManager().getDrawableByName(resName) ?: return
                view.setImageDrawable(drawable)
            }
        }
    },
    GIF_SRC("gifSrc") {
        override fun apply(view: View, resName: String) {
            if (view is GifImageView) {
                resourceManager().getGifDrawableByName(resName)?.let {
                    view.setImageDrawable(it)
                }
            }
        }
    },
    DIVIDER("divider") {
        override fun apply(view: View, resName: String) {
            if (view is ListView) {
                val divider = resourceManager().getDrawableByName(resName) ?: return
                view.divider = divider
            }
        }
    };


    abstract fun apply(view: View, resName: String)

    fun resourceManager(): ResourceManager = SkinManager.getInstance().resourceManager()
}